package com.zrx.app.android.fragment.calculators;

import android.os.Bundle;
import android.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.zrx.app.android.R;

import java.text.DecimalFormat;
/*中融信 计算页面*/

/*里面分为填写部分和结果计算显示部分*/
public class Cal0Frag extends CalBase {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public Cal0Frag() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment Cal0Frag.
     */
    // TODO: Rename and change types and number of parameters
    public static Cal0Frag newInstance() {
        Cal0Frag fragment = new Cal0Frag();
        return fragment;
    }

    EditText carPriceText, principleText, gpsText, insuranceText, serviceFeeText, mortgageFeeText, otherFeeText;
    TextView out2yrsMonthPaymentText,out3yrsMonthPaymentText
            ,out2yrsInterestText,out3yrsInterestText
            ,out2yrsPreInterest,out3yrsPreInterest
            ,out2yrsDownPayment,out3yrsDownPayment
            ,out2yrsSumPayment,out3yrsSumPayment
            ,out2yrsWithoutPreInterest,out3yrsWithoutPreInterest
            ,out2yrsDownPaymentRatio,out3yrsDownPaymentRatio
            ,out2yrsLoanRatio,out3yrsLoanRatio
            ,out2yrsPriceInPad,out3yrsPriceInPad;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_cal0, container, false);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        MyTextWatcher myTextWatcher=new MyTextWatcher();

        carPriceText = (EditText) view.findViewById(R.id.cal0_CarPrice);
        carPriceText.addTextChangedListener(myTextWatcher);
        principleText = (EditText) view.findViewById(R.id.cal0_principle);
        principleText.addTextChangedListener(myTextWatcher);
        gpsText = (EditText) view.findViewById(R.id.cal0_gps);
        gpsText.addTextChangedListener(myTextWatcher);
        insuranceText = (EditText) view.findViewById(R.id.cal0_insurance);
        insuranceText.addTextChangedListener(myTextWatcher);
        serviceFeeText = (EditText) view.findViewById(R.id.cal0_service_fee);
        serviceFeeText.addTextChangedListener(myTextWatcher);
        mortgageFeeText = (EditText) view.findViewById(R.id.cal0_mortgage_fee);
        mortgageFeeText.addTextChangedListener(myTextWatcher);
        otherFeeText = (EditText) view.findViewById(R.id.cal0_other_fee);
        otherFeeText.addTextChangedListener(myTextWatcher);

        //out
        out2yrsMonthPaymentText=(TextView)view.findViewById(R.id.cal0_2year_monthPayment);
        out3yrsMonthPaymentText=(TextView)view.findViewById(R.id.cal0_3year_monthPayment);

        out2yrsInterestText=(TextView)view.findViewById(R.id.cal0_2year_interest);
        out3yrsInterestText=(TextView)view.findViewById(R.id.cal0_3year_interest);

        out2yrsPreInterest=(TextView)view.findViewById(R.id.cal0_2year_pre_interest);
        out3yrsPreInterest=(TextView)view.findViewById(R.id.cal0_3year_pre_interest);

        out2yrsDownPayment=(TextView)view.findViewById(R.id.cal0_2year_down_payment);
        out3yrsDownPayment=(TextView)view.findViewById(R.id.cal0_3year_down_payment);

        out2yrsSumPayment=(TextView)view.findViewById(R.id.cal0_2year_sum_payment);
        out3yrsSumPayment=(TextView)view.findViewById(R.id.cal0_3year_sum_payment);

        out2yrsWithoutPreInterest=(TextView)view.findViewById(R.id.cal0_2year_without_pre_interest);
        out3yrsWithoutPreInterest=(TextView)view.findViewById(R.id.cal0_3year_without_pre_interest);

        out2yrsDownPaymentRatio=(TextView)view.findViewById(R.id.cal0_2year_down_payment_ratio);
        out3yrsDownPaymentRatio=(TextView)view.findViewById(R.id.cal0_3year_down_payment_ratio);

        out2yrsLoanRatio=(TextView)view.findViewById(R.id.cal0_2year_loan_ratio);
        out3yrsLoanRatio=(TextView)view.findViewById(R.id.cal0_3year_loan_ratio);

        out2yrsPriceInPad=(TextView)view.findViewById(R.id.cal0_2year_price_in_pad);
        out3yrsPriceInPad=(TextView)view.findViewById(R.id.cal0_3year_price_in_pad);

        return view;
    }

    void calculate(){
        try{
            double carPrice=Double.valueOf(carPriceText.getText().toString());
            double principle=Double.valueOf(principleText.getText().toString());
            double gps=Double.valueOf(gpsText.getText().toString());
            double insurance=Double.valueOf(insuranceText.getText().toString());
            double serviceFee=Double.valueOf(serviceFeeText.getText().toString());
            double mortgageFee=Double.valueOf(mortgageFeeText.getText().toString());
            double otherFee=Double.valueOf(otherFeeText.getText().toString());

            double m2yrsInterest=24*principle*0.0068;
            double m3yrsInterest=36*principle*0.0068;

            out2yrsMonthPaymentText.setText(nf.format((principle+m2yrsInterest)/24.));
            out3yrsMonthPaymentText.setText(nf.format((principle+m2yrsInterest)/36.));

            out2yrsInterestText.setText(nf.format(m2yrsInterest));
            out3yrsInterestText.setText(nf.format(m3yrsInterest));

            double m2yrsPreInterest=principle*24*0.00137;
            double m3yrsPreInterest=principle*36*0.00137;
            out2yrsPreInterest.setText(nf.format(m2yrsPreInterest));
            out3yrsPreInterest.setText(nf.format(m3yrsPreInterest));

            double downPayment=carPrice-principle;
            out2yrsDownPayment.setText(nf.format(downPayment));
            out3yrsDownPayment.setText(nf.format(downPayment));

            double m2yrsSumPayment=downPayment+gps+insurance+serviceFee+mortgageFee+otherFee+m2yrsPreInterest;
            double m3yrsSumPayment=downPayment+gps+insurance+serviceFee+mortgageFee+otherFee+m3yrsPreInterest;
            out2yrsSumPayment.setText(nf.format(m2yrsSumPayment));
            out3yrsSumPayment.setText(nf.format(m3yrsSumPayment));

            out2yrsWithoutPreInterest.setText(nf.format(m2yrsSumPayment-m2yrsPreInterest));
            out3yrsWithoutPreInterest.setText(nf.format(m3yrsSumPayment-m3yrsPreInterest));

            out2yrsDownPaymentRatio.setText(nf.format(downPayment/carPrice));
            out3yrsDownPaymentRatio.setText(nf.format(downPayment/carPrice));

            out2yrsLoanRatio.setText(nf.format(principle/carPrice));
            out3yrsLoanRatio.setText(nf.format(principle/carPrice));

            out2yrsPriceInPad.setText(nf.format(principle/0.7));
            out3yrsPriceInPad.setText(nf.format(principle/0.7));
        }catch (Exception e){

        }

    }

}
